package NWish.utils
{
	import NWish.Interface.ILogger;
	import NWish.Misc;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	
	public class NWFile
	{
		//
		private var _openFile:FileReference;
		
		//
		public function NWFile(description:String, extension:String, onOpenFile:Function)
		{
			_openFile = new FileReference();
			_openFile.addEventListener(Event.COMPLETE, onOpenFileHandler);
			_openFile.addEventListener(IOErrorEvent.IO_ERROR, Misc.ioErrorHandler);
			_openFile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, Misc.securityErrorHandler);
			_openFile.addEventListener(Event.SELECT, selectHandler);
			_openFile.browse([new FileFilter(description, extension)]);
		
			
			function onOpenFileHandler(e):void
			{
				var file = e.target;
				onOpenFile(file.data,file);
			}
		}
		
		private function selectHandler(e:Event):void
		{
			ILogger.log("open json file : " + _openFile.name);
			_openFile.load();
		}
	}
}